/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-03-03
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-16
 * @Description: socket 事件类
 */
import 'package:chat_client/enums/chat_enum.dart';
import 'package:chat_client/models/conversation_model.dart';
import 'package:chat_client/models/session_model.dart';

class MessageEvents {
  // 发消息
  static const String messageSend = 'message:send';
  // 发送消息后的回执
  static const String messageReceipt = 'message:receipt';
  // 接收消息
  static const String messageReceive = 'message:receive';
  // 拉取所有远程socket信息
  static const String pullAllConversation = 'conversation:pull-all';
}

class NotifyEvents {
  // 好友申请
  static const String friendRequest = 'notify:friend-request';
  // 加入群聊
  static const String publicGroupJoined = 'notify:public-group-joined';
  static const String groupJoined = 'notify:group-joined';
  // 系统通知
}

// 确认收到消息
class ConfirmEvents {
  static const String message = 'confirm:message';
  static const String notify = 'confirm:notify';
}

// 自定义错误类型，用于处理服务端返回的响应体错误
class SocketError implements Exception {
  final String message;

  SocketError({required this.message});

  @override
  String toString() {
    return message;
  }
}

// 自定义缓存事件类
class CacheEvent {
  final String event;
  final dynamic data;
  CacheEvent({
    required this.event,
    required this.data,
  });
}

// 将message对象转为 Map（空值过滤）
Map<String, dynamic> messageToMap({
  required Message message,
  required SessionModel session,
}) {
  return {
    if (session.conversationId != null)
      'conversationId': session.conversationId,
    'conversationType': session.type,
    if (session.type == ConversationType.user.value) 'receiverId': session.id,
    if (session.type == ConversationType.group.value) 'groupId': session.id,
    'type': message.type,
    'data': message.data,
    if (message.additional != null) 'additional': message.additional,
  };
}

class NtifyContent {
  final String title;
  final String body;
  final String icon;
  final String event;
  final String notifyId;

  NtifyContent({
    required this.title,
    required this.body,
    required this.icon,
    required this.event,
    required this.notifyId,
  });

  factory NtifyContent.fromJson(Map<String, dynamic> json) {
    return NtifyContent(
      title: json['title'] ?? '',
      body: json['body'] ?? '',
      icon: json['icon'] ?? '',
      event: json['event'] ?? '',
      notifyId: json['notifyId'] ?? '',
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'notifyId': notifyId,
    };
  }
}
